package com.own.business.system.parameter.base.frame.business.service.impl;

import com.own.business.system.parameter.base.frame.business.service.SystemParameterService;
import com.own.business.system.parameter.base.frame.entity.bo.SystemParameterBo;
import com.own.business.system.parameter.base.frame.entity.map.SystemParameterMapVo;
import com.own.business.system.parameter.base.frame.entity.po.SystemParameter;
import com.own.business.system.parameter.base.frame.entity.query.SystemParameterQuery;
import com.own.business.system.parameter.base.frame.entity.vo.SystemParameterVo;
import com.own.business.system.parameter.base.frame.mapper.SystemParameterMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.common.util.list.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SystemParameterServiceImpl
 *
 * @author chenxueli
 * @date 2023-06-29 09:42:59
 */
@Slf4j
@Service
public class SystemParameterServiceImpl extends AbstractBaseService<
        SystemParameter,
        SystemParameterBo,
        SystemParameterVo,
        SystemParameterMapVo,
        SystemParameterQuery,
        SystemParameterMapper
        > implements SystemParameterService {

    @Resource
    private SystemParameterMapper systemParameterMapper;

    /**
     * 根据code和env获取系统参数
     *
     * @param code 系统参数code
     * @param env  系统参数env
     * @return 系统参数
     */
    @Override
    public SystemParameter getByCodeAndEnv(String code, String env) {
        var wrapper = lambdaQueryWrapper().eq(SystemParameter::getParameterCode, code);
        var list = systemParameterMapper.selectList(wrapper);
        return list.stream().filter(item -> {
            if (StringUtils.isBlank(item.getEffectiveEnvironment())) {
                return true;
            }
            var envs = ListUtil.Spilt.to(item.getEffectiveEnvironment(), ",");
            return envs.contains(env);
        }).findFirst().orElse(null);
    }

    /**
     * 根据code和env获取系统参数列表
     *
     * @param group 系统参数group
     * @param env   系统参数env
     * @return 系统参数
     */
    @Override
    public List<SystemParameter> listByGroupAndEnv(String group, String env) {
        var wrapper = lambdaQueryWrapper().eq(SystemParameter::getParameterGroup, group);
        var list = systemParameterMapper.selectList(wrapper);
        return list.stream().filter(item -> {
            if (StringUtils.isBlank(item.getEffectiveEnvironment())) {
                return true;
            }
            var envs = ListUtil.Spilt.to(item.getEffectiveEnvironment(), ",");
            return envs.contains(env);
        }).collect(Collectors.toList());
    }

}
